<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：微折促销管理信息系统
// 代码日期：v3.0 2018-12-20
// 作者：杨道泉 
// 文件名：PageAdminController.class.php 
// 功能： 管理手机页面模板

class PageAdminController extends QnuiBaseController {
	public function __construct(){
		parent::__construct();
		if(!(session("VISITOR_NICK")=="zlp130" || session("VISITOR_NICK")=="kekydream"))
	    {
		  $err = C("ErrCode");
		  $resp = new \stdClass();
		  $resp->code = 917;
		  $resp->msg = $err[917];
		  $this->ajaxReturn(pre_resp($resp));
		  die();
	    }
	}
	public function save_as_model(){
		$page_id  = I("page_id");
		//$PG = M("PageInshop");
		//$page = current($PG->where(Array("id"=>$page_id))->getField("id,name,preview,qrcode_url"));
		$MD = M("PageModels");
		$model['tag_color']=I("tag_color");
		$model['tag_time_nodes']=I("tag_time_nodes");
		$model['tag_action']=I("tag_action");
		$model['name']=I("name");
		$model['price_v1']=I("price_v1");
		$model['price_v2']=I("price_v2");
		$model['price_v3']=I("price_v3");
		$model['price_v4']=I("price_v4");
		$model['price_v5']=I("price_v5");
		$model['info']=I("info");
		$model['preview']=I("preview");
		$model['preview_page_id']=$page_id;
		$model['qrcode']=I("qrcode");
		$model['item_code_v1']=I("item_code_v1");
		$model['item_code_v2']=I("item_code_v2");
		$model['item_code_v3']=I("item_code_v3");
		$model['item_code_v4']=I("item_code_v4");
		$model['item_code_v5']=I("item_code_v5");
		$model['creat_date']=date("Y-m-d H:i:s");
		$model['catgory']=I("catgory");
		$model['status']="normal";
		$model['js_tpl']="";
		$model_id = $MD->add($model);
		
		$COMP = M("PageComponentsInpage");
		$page_comp = $COMP->where(Array("page_id"=>$page_id))->getField("id,name,mix,cat,max_count,code,ordinal,model_private,status");
		$COMP_SET = M("PageComponentSettingsInpage");
		$MDCOMP = M("PageComponentsInmodel");
		$MDCOMP_SET = M("PageComponentSettings");
		foreach($page_comp as $p_c){
			$md_comp = Array();
			$md_comp['model_id']=$model_id;
			$md_comp['name']=$p_c["name"];
			$md_comp['cat']=$p_c["cat"];
			$md_comp['comp_code']=$p_c["code"];
			$md_comp['creat_date']=date("Y-m-d H:i:s");
			$md_comp['ordinal']=$p_c["ordinal"];
			$md_comp['model_private']=$p_c["model_private"];
			$md_comp['max_count']=$p_c["max_count"];
			$md_comp['mix']=$p_c["mix"];
			$md_comp['status']=$p_c["status"];
			$comp_id = $MDCOMP->add($md_comp);
			$page_comp_settings = $COMP_SET->where(Array("component_inpage_id"=>$p_c["id"]))->getField("id,field_name,name,val");
			
			$md_comp_set_all = Array();
			foreach($page_comp_settings as $p_c_s){
				$md_comp_set = Array();
				$md_comp_set["component_id"] = $comp_id;
				$md_comp_set["field_name"] = $p_c_s["field_name"];
				$md_comp_set["name"] = $p_c_s["name"];
				$md_comp_set["val"] = $p_c_s["val"];
				$md_comp_set_all[]=$md_comp_set;
			}
			$MDCOMP_SET->addAll($md_comp_set_all);
		}
		$resp = new \stdClass();
		$resp->msg = "成功转换成为模板！";
		$resp->model_id = $model_id;
		$this->ajaxReturn(pre_resp($resp));
	}

}